﻿#include "user.h"
#include "errorcode.h"
#include "routine/global.h"
#include "ui/bridge/html_bridge.h"

cls_user::cls_user()
{
    _INIT_LOCK( &m_lock );
    memset( &m_info, 0, sizeof(user_info) );
}

cls_user::~cls_user()
{
    memset( &m_info, 0, sizeof(user_info) );
    _DEL_LOCK( &m_lock );
}

/**
 *	get user name
 *
 * 	@name			[out] the user name buf
 * 	@size			[in] the buf size
 *
 * 	return:
 * 		==	PEM_OK		successful
 * 		!=	PEM_OK		errorcode
 *
 * 	remark:
 * 		if the return value is not PEM_OK, the content of name is undefine
 */
pem_error_t	cls_user::get_user_name( pem_s8_t* name, pem_s32_t size )
{
    pem_error_t		res;

    _LOCK( &m_lock );
    res = _get_user_name( name, size );
    _UNLOCK( &m_lock );

    return res;
}

pem_error_t cls_user::_get_user_name( pem_s8_t* name, pem_s32_t size )
{
    if ( !m_info.active )
        return BUILD_ERRORCODE( 0, PEM_USER_NOACCIVE );

    if ( NAME_LEN > size )
        strncpy( name, m_info.name, size );
    else
        strncpy( name, m_info.name, NAME_LENGTH );

    return PEM_OK;
}

/**
 *	get user right
 *
 * 	@right			[out] the user right poinger
 *
 * 	return:
 * 		==	PEM_OK		successful
 * 		!=	PEM_OK		errorcode
 *
 * 	remark:
 * 		if the return value is not PEM_OK, the content of right is undefine
 */
pem_error_t	cls_user::get_user_right( pem_s32_t* right )
{
    pem_error_t	res;

    _LOCK( &m_lock );
    res = _get_user_right( right );
    _UNLOCK( &m_lock );

    return res;
}

pem_error_t	cls_user::_get_user_right( pem_s32_t* right )
{
    if ( !m_info.active )
        return BUILD_ERRORCODE( 0, PEM_USER_NOACCIVE );

    *right = m_info.right;

    return PEM_OK;
}

/**
 *	set user info into "m_info"
 *
 * 	@info			[in]the user info structure pointer
 * 	@flag			[in]the flag is combined by the value in ENUM_USER eg. USER_NAME | USER_RIGHT
 * 						each bit is set, the corresponding member of user_info struct will be set.
 *
 * 	return:
 * 		==	PEM_OK		successful
 * 		!=	PEM_OK		errorcode
 */
pem_error_t	cls_user::set_user_info( user_info* info, pem_s32_t flag )
{
    pem_error_t		res;

    _LOCK( &m_lock );
    res = _set_user_info( info, flag );
    _UNLOCK( &m_lock );

    return res;
}

pem_error_t	cls_user::_set_user_info( user_info* info, pem_s32_t flag )
{
    if ( flag & USER_NAME )
        strncpy( m_info.name, info->name, sizeof(m_info.name) );

    if ( flag & USER_RIGHT )
        m_info.right 	= info->right;

    if ( flag & USER_ACTIVE )
        m_info.active 	= info->active;

    return PEM_OK;
}

/**
 *	load user info from db
 *
 * 	@obj_bridge			[in] the cls_bridge object pointer
 *
 * 	return
 * 		==	0	successful
 *		!=	0	errorcode
 */
pem_error_t cls_user::load_user_info_from_db( pem_void_t* obj_bridge )
{
    pem_error_t			res 		= PEM_OK;
    cls_bridge&			bridge		= *(cls_bridge*)obj_bridge;
    QString				js_fun;

    js_fun.sprintf( "callback_set_user_info( %d, \"%s\", %d, %d );", 0, "administrator", 0, 1 );

    bridge.call_js_routine( js_fun );

    return res;
}
